package com.NetWork.lesson4;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//模拟下载资源
public class URLDown {
    public static void main(String[] args) throws IOException {
        //下载地址
        URL url = new URL("https://m801.music.126.net/20210313130455/b470d47e65f08850ad36efd70290bcd8/jdyyaac/000f/0f59/5252/7e94d6617383274389b5e84937052d0e.m4a");
        //连接到这个资源
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //
        InputStream is = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("suyan.m4a");
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();
        urlConnection.disconnect();
    }
}

    